<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
set_time_limit(0);

class GamesController extends AppController {

    var $name = 'Games';
    var $components = array(
        'Session', 'Alias', 'Upload', 'Flash'
    );
    var $helpers = array(
        'Editor', 'Published', 'Hotgame', 'Hotgamedaily', 'Hotgameincat', 'Populargameincat', 'Popular'
    );
    var $uses = array(
        'Game', 'Catgame'
    );

    function index() {
        $data = $this->data;
        $this->set('title_for_layout', Configure::read('site_name') . ' - Games');
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        $cat_id = array_key_exists('cat_id', $this->params['named']) ? $this->params['named']['cat_id'] : false;
        $group_id = array_key_exists('group_id', $this->params['named']) ? $this->params['named']['group_id'] : false;
        $search = !empty($this->params['form']['search']) ? $this->params['form']['search'] : false;
        $conditions = array();

        //search
        if ($search) {
            $conditions['OR'] = array(
                'Game.title LIKE' => "%$search%",
                'Game.id LIKE' => "%$search%",
            );
        }
        if (!empty($cat_id)) {
            $conditions['AND'] = array(
                'Game.cat_id' => $cat_id
            );
        }
        if (!empty($group_id)) {
            $conditions['AND'] = array(
                'Game.group_id' => $group_id
            );
        }
        if (!empty($data['cat_id'])) {
            $cat_id = $data['cat_id'];
            $conditions['AND'] = array(
                'Game.cat_id' => $data['cat_id']
            );
        }
        if (!empty($data['group_id'])) {
            $group_id = $data['group_id'];
            $conditions['AND'] = array(
                'Game.group_id' => $data['group_id']
            );
        }
        $this->paginate = array(
            'conditions' => $conditions,
            'limit' => 150,
            'order' => array(
                'Game.incat_ordering' => 'ASC'
            ),
        );

        $data = $this->paginate('Game');
        $this->set('cat_id', $cat_id);
        $this->set('group_id', $group_id);
        $this->set('game_cat_all', $this->Game->Catgame->getListAll());
        $this->set('game_group_all', $this->Game->game_groups->getListAll());
        $this->set('total', $this->params['paging']['Game']['count']);
        $this->set('total_inpage', sizeof($data));
        $this->set('data', $data);
        $this->set('keyword', $search);

        $this->set('limit_page', $this->paginate['limit']);
        $this->set('start_page', $page);
        $this->Game->writeFooter();
    }

    function remove() {
        exit;
        $conditions = array(
            "{$this->Game->alias}.{$this->Game->primaryKey} IN (" . implode(',', $this->params['form']['cid']) . ')'
        );

        //delete flash
        $data = $this->Game->find('list', array(
                    'conditions' => $conditions,
                    'fields' => array('Game.folder_path', 'Game.filename')
                        )
        );

        foreach ($data AS $folder => $filename) {
            //delete it
            if (is_file(Configure::read('game_flash_absolute_path') . $folder . $filename)) {
                $this->Upload->delete(Configure::read('game_flash_absolute_path') . $folder . $filename);
            }
        }

        //delete flash
        $data = $this->Game->find('list', array(
                    'conditions' => $conditions,
                    'fields' => array('Game.folder_path', 'Game.thumbnail')
                        )
        );
        foreach ($data AS $folder => $filename) {
            //delete it
            if (is_file(Configure::read('game_flash_absolute_path') . $folder . $filename)) {
                $this->Upload->delete(Configure::read('game_flash_absolute_path') . $folder . $filename);
            }
        }
        if ($this->Game->deleteAll($conditions)) {
            $this->Session->setFlash(sprintf("Xóa thành công %d Game", sizeof($this->params['form']['cid'])), 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash('Không thể xóa', 'msg', array(), 'error');
        }
        $this->redirect(array('action' => 'index'));
    }

    function updateall() {
        $data = $this->Game->get_all();
        foreach ($data AS $row) {
            $flash_info = $this->Flash->readCreativeDetails(Configure::read('game_flash_absolute_path') . $row['Game']['folder_path'] . $row['Game']['filename']);
            $data_update = array();
            $data_update['id'] = $row['Game']['id'];
            $data_update['game_width'] = $flash_info['width'];
            $data_update['game_height'] = $flash_info['height'];
            $data_update['flash_version'] = $flash_info['pluginVersion'];
            $this->Game->save($data_update);
        }
        exit;
    }

    function update_play_times() {
        $data = $this->Game->get_all();
        foreach ($data AS $row) {
            $data_update = array();
            $data_update['id'] = $row['Game']['id'];
            $data_update['play_times'] = rand(2000, 200000);
            $this->Game->save($data_update);
        }
        exit;
    }

    function update_tags() {
        $data = $this->Game->get_all();
        foreach ($data AS $row) {
            $data_update = array();
            $data_update['id'] = $row['Game']['id'];
            //save tags
            if (!empty($row['Game']['tags'])) {
                $tags_array = explode(',', $row['Game']['tags']);
                $tags_content_html = array();
                foreach ($tags_array AS $tags_name) {
                    $tags_content_html[] = '<a href="http://thegioigamevui.com/tim-kiem-nhanh.html?q=' . $tags_name
                            . '&amp;cx=001897934961332779774%3Agqz6vv_1uze&amp;cof=FORID%3A10&amp;ie=UTF-8" title="' . $tags_name . '">' . $tags_name . '</a>';
                }
                $data_update['tags_html'] = implode(',', $tags_content_html);
            }

            $this->Game->save($data_update);
        }
        exit;
    }

    function edit() {
        $params = array_merge($this->params['form'], $this->params['named']);

        if (isset($params['cid']) && is_array($params['cid'])) {
            $this->Game->id = $params['cid'][0];
        } else {
            $this->Game->id = array_key_exists('id', $params) ? $params['id'] : 0;
        }

        $this->set('title_for_layout', Configure::read('site_name') . ' - Chỉnh sữa thông tin');
        $fields = array(
            '*'
        );

        $this->data = $this->Game->read($fields);

        $this->set('description', $this->data['Game']['description']);
        $this->set('html_content', $this->data['Game']['html_content']);
        $this->set('content_type', $this->data['Game']['content_type']);
        $check_thumbnail_exist = is_file(Configure::read('game_flash_absolute_path') . $this->data['Game']['folder_path'] . $this->data['Game']['thumbnail']);
        $this->set('check_thumbnail_exist', $check_thumbnail_exist);
        $this->set('image_url', Configure::read('game_flash_thumbnail_url_path') . $this->data['Game']['folder_url'] . $this->data['Game']['thumbnail']);

        $check_flash_exist = is_file(Configure::read('game_flash_absolute_path') . $this->data['Game']['filename']);
        $this->set('check_flash_exist', $check_flash_exist);
        $this->set('flash_url', Configure::read('game_flash_url_path') . $this->data['Game']['folder_url'] . $this->data['Game']['filename']);

        $this->set('cat_data', $this->Game->Catgame->getListAll());
        $this->set('group_data', $this->Game->game_groups->getListAll());

        //build content type list
        $contenttype_list = array(
            'swf' => 'Flash',
            'java' => 'Java',
            'dcr' => 'Shockwave',
            'html' => 'Code',
        );
        $this->set('contenttype_list', $contenttype_list);
    }

    function add() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - thêm game');
        $this->set('cat_data', $this->Game->Catgame->getListAll());
        $this->set('group_data', $this->Game->game_groups->getListAll());

        //build content type list
        $contenttype_list = array(
            'swf' => 'Flash',
            'java' => 'Java',
            'dcr' => 'Shockwave',
            'html' => 'Code',
        );
        $this->set('contenttype_list', $contenttype_list);
    }

    function save() {
        $this->Game->writeHomeGameBlock('14', 'blockgame-hanhdong');
        $this->Game->writeHomeGameBlock('35', 'blockgame-phieuluu');
        $this->Game->writeHomeGameBlock('23', 'blockgame-dantran');
        $this->Game->writeHomeGameBlock('16', 'blockgame-vuinhon', true);
        $this->Game->writeHomeGameBlock('34', 'blockgame-kheoleo');
        $this->Game->writeHomeGameBlock('36', 'blockgame-tritue');
        $this->Game->writeHomeGameBlock('29', 'blockgame-duaxe');
        $this->Game->writeHomeGameBlock('20', 'blockgame-chiendau');
        $this->Game->writeHomeGameBlock('44', 'blockgame-xacuop');
        $this->Game->writeLastestGame();
        $this->Game->writeGamehotdisplay();
        $this->Game->writeGamehotdaily();
        $this->Game->writeGamehotdisplaynone();

        $data = $this->data['Game'];
        $row = $this->Game->findById($this->data['Game']['id']);

        //save tags
        if (!empty($data['tags'])) {
            $tags_array = explode(',', $data['tags']);
            $tags_content_html = array();
            foreach ($tags_array AS $tags_name) {
                $tags_content_html[] = '<a href="http://thegioigamevui.com/tim-kiem-nhanh.html?q=' . $tags_name
                        . '&amp;cx=001897934961332779774%3Agqz6vv_1uze&amp;cof=FORID%3A10&amp;ie=UTF-8" title="' . $tags_name . '">' . $tags_name . '</a>';
            }
            $data['tags_html'] = implode(',', $tags_content_html);
        }

        if (!empty($row['Game']['folder_path'])) {
            $folder_path = $row['Game']['folder_path'];
            $path_file_need_delete = Configure::read('game_flash_absolute_path') . $row['Game']['folder_path'];
        } else {
            $folder_path = $this->Upload->createFolder();
            $path_file_need_delete = Configure::read('game_flash_absolute_path') . $folder_path;
        }

        $data['folder_path'] = $folder_path;
        $data['folder_url'] = str_replace('\\', '/', $folder_path);

        //for upload game flash
        $this->Upload->setUploadpath($path_file_need_delete);
        $this->Upload->setRules('size', Configure::read('content_image_maxsize'));
        $this->Upload->setRules('ext', Configure::read('content_image_ext'));

        if ($this->data['Game']['filename']['name'] != '') {
            if ($this->Upload->validate($this->data['Game']['filename'])) {
                //delete it
                if (is_file($path_file_need_delete . $row['Game']['filename'])) {
                    $this->Upload->delete($path_file_need_delete . $row['Game']['filename']);
                }
                $data['filename'] = $this->Upload->upload($this->data['Game']['filename'], false);
            } else {
                $this->Session->setFlash($this->Upload->getError(), 'error', array(), 'msg');
                $this->redirect(array('controller' => 'games', 'action' => 'index'));
                return;
            }
        } else {
            unset($data['filename']);
        }
        //end upload game
        //upload thumbnail
        if ($this->data['Game']['thumbnail']['name'] != '') {
            if ($this->Upload->validate($this->data['Game']['thumbnail'])) {
                //delete it
                if (is_file($path_file_need_delete . $row['Game']['thumbnail'])) {
                    $this->Upload->delete($path_file_need_delete . $row['Game']['thumbnail']);
                }
                $data['thumbnail'] = $this->Upload->upload($this->data['Game']['thumbnail'], false);
            } else {
                $this->Session->setFlash($this->Upload->getError(), 'error', array(), 'msg');
                $this->redirect(array('controller' => 'games', 'action' => 'index'));
                return;
            }
        } else {
            unset($data['thumbnail']);
        }


        if (!empty($data['id'])) {
            $data['user_modified'] = $this->Auth->user('id');
            $data['user_modified_ip'] = $_SERVER['REMOTE_ADDR'];
            
            if ($this->data['Game']['content_type'] == 'swf') {
                $flash_info = $this->Flash->readCreativeDetails($path_file_need_delete . $row['Game']['filename']);
                $data['game_width'] = $flash_info['width'];
                $data['game_height'] = $flash_info['height'];
                //$data['flash_version'] = $flash_info['pluginVersion'];
            }
            
        } else {
            $data['ordering'] = $this->Game->nextOrder();
            $data['incat_ordering'] = $this->Game->nextOrder(array('Game.cat_id' => $data['cat_id']), 'incat_ordering');
            $data['user_created'] = $this->Auth->user('id');
            $data['user_created_ip'] = $_SERVER['REMOTE_ADDR'];
            
            if ($this->data['Game']['content_type'] == 'swf') {
                $flash_info = $this->Flash->readCreativeDetails($path_file_need_delete . $this->data['Game']['filename']);
                $data['game_width'] = $flash_info['width'];
                $data['game_height'] = $flash_info['height'];
                $data['flash_version'] = $flash_info['pluginVersion'];
            }
        }



        $data['alias'] = $this->Alias->alias($data['title']);
        if ($this->Game->save($data)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Game->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

    function cancel() {
        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

    function populargameincat() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'popular_incat' => 1,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );
        }

        if ($this->Game->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Game->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

    function unpopulargameincat() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'popular_incat' => 0,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );
        }

        if ($this->Game->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Game->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

    function hotgameincat() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'hot_game_incat' => 1,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );
        }

        if ($this->Game->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Game->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

    function unhotgameincat() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'hot_game_incat' => 0,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );
        }

        if ($this->Game->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Game->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

    function hotgamedaily() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'hot_game_daily' => 1,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );
        }

        if ($this->Game->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Game->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

    function unhotgamedaily() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'hot_game_daily' => 0,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );
        }

        if ($this->Game->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Game->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

    function hotgame() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'hot_game' => 1,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );
        }

        if ($this->Game->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Game->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

    function unhotgame() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'hot_game' => 0,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );
        }

        if ($this->Game->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Game->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

    function popular() {
        $cid = $this->params['form']['cid'];

        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'popular' => 1,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );
        }

        if ($this->Game->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Game->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

    function unpopular() {
        $cid = $this->params['form']['cid'];

        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'popular' => 0,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );
        }

        if ($this->Game->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Game->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

    function published() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 1,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );
        }

        if ($this->Game->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Game->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

    function unpublished() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 0,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );
        }

        if ($this->Game->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Game->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

    function saveorder() {
        $ordering = $this->params['form']['order'];

        foreach ($ordering AS $id => $order) {
            $fields = array(
                'ordering' => $order,
                'id' => $id,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );

            $this->Game->saveAll($fields);
        }
        $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');

        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

    function saveinorder() {
        $ordering = $this->params['form']['incat_order'];

        foreach ($ordering AS $id => $order) {
            $fields = array(
                'incat_ordering' => $order,
                'id' => $id,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );

            $this->Game->saveAll($fields);
        }
        $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');

        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

    function savepopulargameincatorder() {
        $ordering = $this->params['form']['populargameincat_order'];

        foreach ($ordering AS $id => $order) {
            $fields = array(
                'popular_incat_ordering' => $order,
                'id' => $id,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );

            $this->Game->saveAll($fields);
        }
        $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');

        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

    function savehotgameorder() {
        $ordering = $this->params['form']['hotgame_order'];

        foreach ($ordering AS $id => $order) {
            $fields = array(
                'hot_game_ordering' => $order,
                'id' => $id,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );

            $this->Game->saveAll($fields);
        }
        $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');

        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

    function savehotgameincatorder() {
        $ordering = $this->params['form']['hotgameincat_order'];

        foreach ($ordering AS $id => $order) {
            $fields = array(
                'hot_game_incat_ordering' => $order,
                'id' => $id,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );

            $this->Game->saveAll($fields);
        }
        $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');

        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

    function savehotgamedailyorder() {
        $ordering = $this->params['form']['hotgamedaily_order'];

        foreach ($ordering AS $id => $order) {
            $fields = array(
                'hot_game_daily_ordering' => $order,
                'id' => $id,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );

            $this->Game->saveAll($fields);
        }
        $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');

        $this->redirect(array('controller' => 'games', 'action' => 'index'));
    }

}

